#include<stdio.h>
#include<unistd.h>
#include<sys/types.h> 
#include<sys/wait.h>
#include<stdlib.h>

int main()
{
    pid_t cpid = fork();
    if (cpid == 0) {        // child process
        int count = 5;
        while (count) {     // 子进程运行5s
            printf("child is running: %ds, pid: %d, ppid: %d\n", count--, getpid(), getppid());
            sleep(1);
        }
        printf("child quit...!\n");
        exit(123);          // 终止子进程，退出码为123
    }
    else if (cpid > 0) {    // father process
        int status = 0;                       // 进程退出状态
        
        pid_t ret = waitpid(-1, &status, 0);  // 等待子进程终止
        
        int exit_code = (status >> 8) & 0xff; // 计算子进程的退出码
        int sign = status & 0x7f;             // 计算子进程的终止信号 ��
        
        // 输出子进程id、退出码、终止信号
    	printf("father waits for success, ret: %d, exit code: %d, sign: %d\n", ret, exit_code, sign);
    }
    else {
        // fork failure
    }
    return 0;
}
